package com.donfu.web.controller.house;

import java.util.List;

import com.donfu.system.domain.house.HouseAboutModen;
import com.donfu.system.domain.house.HouseStoreArticle;
import com.donfu.system.service.house.IHouseStoreArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.donfu.common.annotation.Log;
import com.donfu.common.core.controller.BaseController;
import com.donfu.common.core.domain.AjaxResult;
import com.donfu.common.enums.BusinessType;
import com.donfu.common.utils.poi.ExcelUtil;
import com.donfu.common.core.page.TableDataInfo;

/**
 * 店铺内的文章Controller
 * 
 * @author donfu
 * @date 2021-12-09
 */
@Api(tags = "店铺内的文章")
@RestController
@RequestMapping("/house/article")
public class HouseStoreArticleController extends BaseController
{
    @Autowired
    private IHouseStoreArticleService houseStoreArticleService;

    /**
     * 查询店铺内的文章列表
     */
    @ApiOperation("查询店铺内文章列表")
    @PreAuthorize("@ss.hasPermi('house:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(HouseStoreArticle houseStoreArticle)
    {
        startPage();
        List<HouseStoreArticle> list = houseStoreArticleService.selectHouseStoreArticleList(houseStoreArticle);
        return getDataTable(list);
    }

    /**
     * 获取店铺内的文章详细信息
     */
    @ApiOperation("获取文章详情")
    @PreAuthorize("@ss.hasPermi('house:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(houseStoreArticleService.selectHouseStoreArticleById(id));
    }

    /**
     * 新增店铺内的文章
     */
    @ApiOperation("新增文章")
    @PreAuthorize("@ss.hasPermi('house:article:add')")
    @Log(title = "店铺内的文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseStoreArticle houseStoreArticle)
    {
        AjaxResult ajaxResult = checkHouseStoreArticle(houseStoreArticle);
        String code = ajaxResult.get("code").toString();
        if(code.contains("50")){
            return ajaxResult;
        }
        return toAjax(houseStoreArticleService.insertHouseStoreArticle(houseStoreArticle));
    }

    /**
     * 修改店铺内的文章
     */
    @ApiOperation("修改文章")
    @PreAuthorize("@ss.hasPermi('house:article:edit')")
    @Log(title = "店铺内的文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseStoreArticle houseStoreArticle)
    {
        if(houseStoreArticle.getId()==null){
            return AjaxResult.error("id不能为空");
        }
        AjaxResult ajaxResult = checkHouseStoreArticle(houseStoreArticle);
        String code = ajaxResult.get("code").toString();
        if(code.contains("50")){
            return ajaxResult;
        }
        return toAjax(houseStoreArticleService.updateHouseStoreArticle(houseStoreArticle));
    }

    /**
     * 删除店铺内的文章
     */
    @ApiOperation("删除文章")
    @PreAuthorize("@ss.hasPermi('house:article:remove')")
    @Log(title = "店铺内的文章", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(houseStoreArticleService.deleteHouseStoreArticleByIds(ids));
    }

    /**
     * 检测参数是否合格
     * @param houseStoreArticle
     * @return
     */
    public AjaxResult checkHouseStoreArticle(HouseStoreArticle houseStoreArticle){
        String title = houseStoreArticle.getTitle();
        if(title==null || title.equals("")){
            return AjaxResult.error("标题不能为空");
        }
        if(title.length()>150){
            return AjaxResult.error("标题名称过长");
        }
        String content = houseStoreArticle.getContent();
        if (content==null || content.equals("")){
            return AjaxResult.error("文章内容不能为空");
        }
        String standby = houseStoreArticle.getStandby();
        if(standby!=null && standby.length()>100){
            return AjaxResult.error("备注过长");
        }
        String smallTitle = houseStoreArticle.getSmallTitle();
        if(smallTitle.length()>150){
            return AjaxResult.error("副标题名称过长");
        }
        return AjaxResult.success();
    }
}
